﻿using ComLib;
using LogLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using ComLib.ComType;
using ComLib.ConfigService;
using ComLib.ComFun;
using System.Threading.Tasks;
using ProcessConfig.ConfigType;
using ProcessConfig;

namespace CardQCProcessor
{
    /// <summary>
    /// 用于截获一所质检弹出窗口并关闭
    /// </summary>
    public class Monitor
    {
        private const int BM_CLICK = 0xF5;

        private QCOptions _options = Configs.QC;

        private QCCheckOption _option = new QCCheckOption();

        private ApplyTo _apply = ApplyTo.ExitAndEntry;

        /// <summary>
        /// 取消令牌，用于指示循环任务是否需要继续
        /// </summary>
        private bool _bCancelToken = false;
        public bool CancelToken
        {
            get { return _bCancelToken; }
            set { _bCancelToken = value; }
        }

        /// <summary>
        /// 等待处理事件
        /// </summary>
        private ManualResetEventSlim _mres = new ManualResetEventSlim(false);

        public Monitor()
        {
            SelectDefaultOption(_apply);
        }

        public void SelectDefaultOption(ApplyTo app)
        {
            _apply = app;
            switch (_apply)
            {
                case ApplyTo.ExitAndEntry:
                    _option = _options.CRJ;
                    break;
                case ApplyTo.IDCard:
                    break;
                case ApplyTo.PortVisa:
                    _option = _options.Visa;
                    break;
                case ApplyTo.SocialSecurity:
                    break;
                default:
                    _option = _options.CRJ;
                    break;
            }
        }

        public void SetEvent()
        {
            _mres.Set();
        }

        public void Reset()
        {
            _mres.Reset();
        }

        public void StartWorking()
        {
            Task.Factory.StartNew(() => 
            {
                Log.WriteAsync("开始监听一所基本合格窗口");
                while (!_bCancelToken)
                {
                    _mres.Wait();
                    if (_bCancelToken)
                    {
                        break;
                    }                   
                    OnWorkItem();
                }
                Log.WriteAsync("停止监听一所基本合格窗口");
            });
        }

        public void StopWorking()
        {
            _bCancelToken = true;
            _mres.Set();
        }

        protected void OnWorkItem()
        {
            //监控一所的基本合格窗口，并且关闭      
            if (_option.UseMode == QCMode.Mandatory)
            {
                HandleMandatoryMode();
            }
            else
            {
                HandleOptionalMode();
            }
        }

        /// <summary>
        /// 处理强制模式
        /// </summary>
        private void HandleMandatoryMode()
        {
            IntPtr mainHwnd = new IntPtr();
            mainHwnd = WinAPI.FindWindow(null, "电子证件相片图像质量检查软件");
            if (mainHwnd != IntPtr.Zero)
            {
                IntPtr childHwnd = WinAPI.FindWindowEx(mainHwnd, IntPtr.Zero, null, "确  定");
                if (childHwnd != IntPtr.Zero)
                {
                    WinAPI.PostMessage(childHwnd, BM_CLICK, 0, 0);
                    Log.WriteAsync("检测到弹出‘基本合格’窗口并关闭.");
                }
            }           
        }

        /// <summary>
        /// 处理可选模式
        /// </summary>
        private void HandleOptionalMode()
        {
            IntPtr mainHwnd = new IntPtr();
            mainHwnd = WinAPI.FindWindow(null, "电子证件相片图像质量检查软件");
            if (mainHwnd == IntPtr.Zero)
            {
                return;
            }
            IntPtr childHwnd = WinAPI.FindWindowEx(mainHwnd, IntPtr.Zero, null, "修改");
            if (childHwnd == IntPtr.Zero)
            {
                return;
            }
            WinAPI.PostMessage(childHwnd, BM_CLICK, 0, 0);
            Log.WriteAsync("点击了修改按钮.");
            IntPtr hwnd = WinAPI.FindWindowEx(mainHwnd, IntPtr.Zero, null, "合  格");
            if (hwnd != IntPtr.Zero)
            {
                WinAPI.PostMessage(hwnd, BM_CLICK, 0, 0);
                Log.WriteAsync("点击了合格按钮.");
            }
        }
    }
}
